﻿using Microsoft.EntityFrameworkCore;
using System.Linq;
using System.Reflection;
using Loong.Auditing;
using Loong.BackgroundJobs;
using Loong.Domain.Uow;
using Loong.EntityFrameworkCore;
using Loong.Runtime.Session;
using Loong.Threading.Locks.Database;

namespace Loong
{
    public class LoongDbContext : DbContextBase
    {
        public DbSet<AuditLog> AuditLogs { get; set; }
        public DbSet<BackgroundJobInfo> BackgroundJobs { get; set; }
        public DbSet<ResourceLock> ResourceLocks { get; set; }

        public LoongDbContext(
            DbContextOptions<LoongDbContext> options,
            ILoongSession session,
            IGuidGenerator guidGenerator,
            ICurrentUnitOfWorkProvider currentUnitOfWorkProvider)
            : base(options, session, guidGenerator, currentUnitOfWorkProvider)
        {
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            modelBuilder.ApplyConfigurationsFromAssembly(
                Assembly.GetExecutingAssembly(),
                t => t.IsClass && t.GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IEntityTypeConfiguration<>)));
        }
    }
}
